load("@rules_gazebo//gazebo:headers.bzl", "gz_configure_header", "gz_export_header")
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [":license"],
    features = [
        "layering_check",
        "parse_headers",
    ],
)

license(
    name = "license",
    package_name = "gz-transport",
)

licenses(["notice"])

exports_files([
    "LICENSE",
    "MODULE.bazel",
    "package.xml",
])

gz_configure_header(
    name = "Config",
    src = "include/gz/transport/config.hh.in",
    out = "include/gz/transport/config.hh",
    package_xml = "package.xml",
)

gz_export_header(
    name = "Export",
    out = "include/gz/transport/Export.hh",
    export_base = "GZ_TRANSPORT",
    lib_name = "gz-transport",
)

public_headers_no_gen = glob([
    "include/gz/transport/*.h",
    "include/gz/transport/*.hh",
    "include/gz/transport/detail/*.hh",
])

private_headers = glob(["src/*.hh"])

sources = glob(
    ["src/*.cc"],
    exclude = [
        "src/*_TEST.cc",
    ],
)

public_headers = public_headers_no_gen + [
    "include/gz/transport/config.hh",
    "include/gz/transport/Export.hh",
]

cc_library(
    name = "gz-transport",
    srcs = sources + private_headers,
    hdrs = public_headers,
    copts = [
        "-Wno-deprecated-declarations",
    ],
    features = [
        # Layering check fails for clang build due to no module exporting
        # `google/protobuf/stubs/common.h`. Unfortunately, the bazel target that
        # exports this header in protobuf is private.
        "-layering_check",
    ],
    includes = ["include"],
    visibility = ["//visibility:public"],
    deps = [
        "@com_google_protobuf//:protobuf",
        "@cppzmq",
        "@gz-msgs",
        "@gz-msgs//:gzmsgs_cc_proto",
        "@gz-utils//:SuppressWarning",
        "@libuuid",
        "@libzmq",
    ],
)

cc_binary(
    name = "topic",
    srcs = [
        "src/cmd/gz.cc",
        "src/cmd/gz.hh",
        "src/cmd/topic_main.cc",
    ],
    data = [
        "@gz-msgs//:gzmsgs_proto",
    ],
    deps = [
        ":gz-transport",
        "@com_google_protobuf//:json_util",
        "@gz-msgs",
        "@gz-utils//cli:GzFormatter",
        "@gz-utils//cli:cli11",
    ],
)

cc_binary(
    name = "service",
    srcs = [
        "src/cmd/gz.cc",
        "src/cmd/gz.hh",
        "src/cmd/service_main.cc",
    ],
    data = [
        "@gz-msgs//:gzmsgs_proto",
    ],
    deps = [
        ":gz-transport",
        "@com_google_protobuf//:json_util",
        "@gz-msgs",
        "@gz-utils//cli:GzFormatter",
        "@gz-utils//cli:cli11",
    ],
)

# test_sources = glob(
#     include = ["src/*_TEST.cc"],
# )

# [cc_test(
#     name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
#     srcs = [src],
#     env = {
#         "GZ_BAZEL": "1",
#         "GZ_BAZEL_PATH": "transport",
#     },
#     deps = [
#         ":transport",
#         GZ_ROOT + "common/testing",
#         GZ_ROOT + "transport/test:utils",
#         "@gtest",
#         "@gtest//:gtest_main",
#     ],
# ) for src in test_sources]
